﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ThiTracNghiem
{
    public partial class FindForm : Form
    {
        DataGridView dgv;
        int startIndex = 0;
        int endIndex;
        int searchDataType = 0;

        public FindForm(DataGridView dgv)
        {
            InitializeComponent();
            cbTimTheo.SelectedIndex = 0;
            this.dgv = dgv;
            endIndex = dgv.Rows.Count;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            lbKetQua.Text = "";
            for (int i = 0; i < endIndex; i++)
            {

                if (dgv.Rows[i].Cells[searchDataType].Value.ToString().ToUpper().IndexOf(txtTuKhoa.Text.ToUpper()) != -1)
                {
                    dgv.Rows[i].Selected = true;
                    startIndex = i + 1;
                    return;
                }
            }

            lbKetQua.Text = "Không tìm thấy kết quả nào!";

        }

        private void btnFindNext_Click(object sender, EventArgs e)
        {
            lbKetQua.Text = "";
            for (int i = startIndex; i < endIndex; i++)
            {
                if (dgv.Rows[i].Cells[searchDataType].Value.ToString().ToUpper().IndexOf(txtTuKhoa.Text.ToUpper()) != -1)
                {
                    dgv.Rows[i].Selected = true;
                    startIndex = i + 1;
                    return;
                }
            }

            lbKetQua.Text = "Không còn kết quả nào!";
        }

        private void txtTuKhoa_TextChanged(object sender, EventArgs e)
        {
            startIndex = 0;
        }

        private void cbTimTheo_SelectedIndexChanged(object sender, EventArgs e)
        {
            startIndex = 0;
            searchDataType = cbTimTheo.SelectedIndex;
            if (cbTimTheo.SelectedIndex == 0)
            {
                txtTuKhoa.MaxLength = 5;
                txtTuKhoa.CharacterCasing = CharacterCasing.Upper;
            }
            else
            {
                txtTuKhoa.MaxLength = 40;
                txtTuKhoa.CharacterCasing = CharacterCasing.Normal;
            }
        }

        public static bool ValidateInput(KeyPressEventArgs e)
        {
            bool blDiscardUserInput = true;

            if (Char.IsLetter(e.KeyChar))
                return false;

            // Backspace or Space
            if (e.KeyChar == 8 || e.KeyChar == (char)Keys.Space)
                blDiscardUserInput = false;

            return blDiscardUserInput;
        }

        private void txtTuKhoa_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = ValidateInput(e);
        }

        private void btnFindPrev_Click(object sender, EventArgs e)
        {

        }
    }
}
